<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('出资人类型')" prop="investorType">
            <el-select v-model="form.investorType" :placeholder="$t('出资人类型')" disabled>
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资人名称')" prop="investorId">
            <MainBodySelect
              v-model="form.investorId"
              disabled
              parameters="lpInvestor"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金名称')" prop="fundId">
            <MainBodySelect
              v-model="form.fundId"
              parameters="fund"
              @change="setFundData"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计认缴时间')" prop="estimatedTime">
            <DatePicker
              v-model="form.estimatedTime"
              :placeholder="$t('预计认缴时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资意向金额') + $t('金额单位')" prop="amountIntention">
            <Money v-model="form.amountIntention" unit="w" :placeholder="$t('投资意向金额')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('募集阶段')" prop="lpStage">
            <dictSelect
              v-model="form.lpStage"
              :clearable="true"
              dictionary="crm_lp_stage"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最终认缴金额') + $t('金额单位')" prop="finalAmount">
            <Money v-model="form.finalAmount" unit="w" :placeholder="$t('最终认缴金额')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最终认缴日期')" prop="finalDate">
            <DatePicker
              v-model="form.finalDate"
              :placeholder="$t('最终认缴日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('银行户名')" prop="bankName">
            <el-input v-model="form.bankName" :placeholder="$t('银行户名')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('银行卡号')" prop="bankNumber">
            <el-input v-model="form.bankNumber" :placeholder="$t('银行卡号')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开户行')" prop="accountBank">
            <el-input v-model="form.accountBank" :placeholder="$t('开户行')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('支行')" prop="branch">
            <el-input v-model="form.branch" :placeholder="$t('支行')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button v-hasPermi="['fund:intentionInvest:edit']" type="primary" @click="submitForm">{{ $t("保存") }}
      </el-button>
    </div>
    <!-- 添加或修改客户管理对话框 -->
    <el-dialog
      :title="$t('添加客户')"
      :visible.sync="formOpen"
      custom-class="fixedDialog"
      width="70%"
      :close-on-click-modal="false"
      :lock-scroll="false"
      append-to-body
    />
  </div>
</template>
<script>
import { saveFundIntentionInvest } from '@/api/fund/intentionInvest/fundIntentionInvest'
import MainBodySelect from '@/components/MainBodySelect/index.vue'
import { getFundBase } from '@/api/fund/fundBase'
import { getLpBase } from '@/api/fund/lpBase/lpBase'

export default {
  components: {
    MainBodySelect
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        investorType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      typeOptions: []
    }
  },
  created() {
    // 类型字典2
    this.getDicts('lp_type').then(response => {
      response.data.forEach(item => {
        this.typeOptions.push(item)
      })
    })
  },
  mounted() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.reset()
      this.form.investorId = data.lpId
      getLpBase(data.lpId).then((res) => {
        if (res.code === 200 && res.data) {
          this.form.investorType = res.data.type
        }
      })
    },
    setFundData(fundId) {
      console.log('选择的基金返回数据fundId', fundId)
      getFundBase(fundId).then((res) => {
        if (res.code === 200 && res.data) {
          this.form.fundName = res.data.fundFullName
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: undefined,
        fundName: undefined,
        investorId: undefined,
        lpStage: '1',
        investorName: undefined,
        investorType: undefined,
        estimatedTime: undefined,
        amountIntention: undefined,
        finalAmount: undefined,
        finalDate: undefined,
        bankName: undefined,
        bankNumber: undefined,
        accountBank: undefined,
        branch: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.intentionOpen = false
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面
      this.$parent.$parent.intentionOpen = false
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundIntentionInvest(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.$emit('sendIntentionInvestData', this.form)
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }
  }
}
</script>
